package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.R;
import com.itheima.reggie.common.ThreadLocalUtils;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.ex.CustomerException;
import com.itheima.reggie.mapper.ShoppingCartMapper;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * @author ghy
 * @version v1.0
 * create on 2022/11/23 11:06
 */
@Service
@Transactional
@Slf4j
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart>
    implements ShoppingCartService {

    /**
     * 从购物车中减去指定菜品的数量
     * @param shoppingCart
     * @return
     */
    @Override
    public R sub(ShoppingCart shoppingCart) {
        //1.根据用户ID和菜品ID或者套餐ID查询
        Long user = ThreadLocalUtils.get();
        if(null == user) {
            throw new CustomerException("登录后，才能点餐");
        }
        shoppingCart.setUserId(user);
        LambdaQueryWrapper<ShoppingCart> lqw = Wrappers.<ShoppingCart>lambdaQuery()
                .eq(ShoppingCart::getUserId, user);
        if(shoppingCart.getDishId() != null) {
            lqw.eq(ShoppingCart::getDishId, shoppingCart.getDishId() );
        } else {
            lqw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId() );
        }
        ShoppingCart cart = this.getOne(lqw);
        //2.根据菜品ID或者套餐ID 减-1
        if(cart == null) {
            throw new CustomerException("购物车不存在");
        }
        Integer number = cart.getNumber();  //购物车中已有数量
        cart.setNumber(number - 1);
        if(cart.getNumber() <= 0) {
            //删除当前数据
            this.removeById(cart.getId());
            cart = new ShoppingCart();
            cart.setUserId(user);
        } else {
            //更新数量到数据库
            cart.setCreateTime(LocalDateTime.now());
            this.updateById(cart);
        }
        //3.把购物车的最新数据返回
        return R.success(cart);
    }

    /**
     * 加入菜品或者套餐到购物车
     * @param shoppingCart
     * @return
     */
    @Override
    public R add(ShoppingCart shoppingCart) {
        //1.获取登录用户ID
        Long user = ThreadLocalUtils.get();
        if(null == user) {
            throw new CustomerException("登录后，才能点餐");
        }
        shoppingCart.setUserId(user);
        LambdaQueryWrapper<ShoppingCart> lqw = Wrappers.<ShoppingCart>lambdaQuery()
                .eq(ShoppingCart::getUserId, user);
        //2.判断加入的是菜品还是套餐
        if(shoppingCart.getDishId() != null) {
            lqw.eq(ShoppingCart::getDishId, shoppingCart.getDishId() );
        } else {
            lqw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId() );
        }
        //3.根据用户ID和加入的菜或者套餐查询购物车对象
        ShoppingCart cart = this.getOne(lqw);
        //4.判断购物车对象是否为空
        if(cart == null) {
            //4.1 空的，没有加过这个菜或者套餐
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            this.save(shoppingCart);
            cart = shoppingCart;
        } else {
            //4.2 不是空的，已经加过，数量+1
            cart.setNumber(cart.getNumber() + 1);
            cart.setCreateTime(LocalDateTime.now());
            //更新回数据库
            this.updateById(cart);
        }
        //5.把最新的购物车数据返回
        return R.success(cart);
    }
}
